import axios from 'axios'
import { useAppStore } from '../stores/app'

const service = axios.create({
    baseURL: '/api', // 使用代理配置
    timeout: 15000
})

// 请求拦截器
service.interceptors.request.use(config => {
    const store = useAppStore()
    if (store.token) {
        config.headers.Authorization = `Bearer ${store.token}`
    }
    return config
})

// 响应拦截器
service.interceptors.response.use(
    response => {
        if (response.data.code !== 0) {
            return Promise.reject(response.data.msg)
        }
        return response.data
    },
    error => {
        return Promise.reject(error)
    }
)

export default service